home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / BitTorrent / zurllib.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  4.4 KB  |  142 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. from urllib import *
  5. from urllib2 import *
  6. from gzip import GzipFile
  7. from StringIO import StringIO
  8. from __init__ import version
  9. import pprint
  10. import config
  11. import prefs
  12. DEBUG = 0
  13.  
  14. class HTTPContentEncodingHandler(HTTPHandler):
  15.     '''Inherit and add gzip/deflate/etc support to HTTP gets.'''
  16.     
  17.     def http_open(self, req):
  18.         req.add_header('Accept-Encoding', 'gzip')
  19.         req.add_header('User-Agent', 'BitTorrent/%s %s/%s (%s)' % (version, config.get(prefs.SHORT_APP_NAME), config.get(prefs.APP_VERSION), config.get(prefs.PROJECT_URL)))
  20.         if DEBUG:
  21.             print 'Sending:'
  22.             print req.headers
  23.             print '\n'
  24.         
  25.         fp = HTTPHandler.http_open(self, req)
  26.         headers = fp.headers
  27.         if DEBUG:
  28.             pprint.pprint(headers.dict)
  29.         
  30.         url = fp.url
  31.         resp = addinfourldecompress(fp, headers, url)
  32.         if 'code' in dir(fp):
  33.             resp.code = fp.code
  34.         
  35.         if 'msg' in dir(fp):
  36.             resp.msg = fp.msg
  37.         
  38.         return resp
  39.  
  40.  
  41.  
  42. class addinfourldecompress(addinfourl):
  43.     '''Do gzip decompression if necessary. Do addinfourl stuff too.'''
  44.     
  45.     def __init__(self, fp, headers, url):
  46.         if headers.has_key('content-encoding') and headers['content-encoding'] == 'gzip':
  47.             if DEBUG:
  48.                 print 'Contents of Content-encoding: ' + headers['Content-encoding'] + '\n'
  49.             
  50.             self.gzip = 1
  51.             self.rawfp = fp
  52.             fp = GzipStream(fp)
  53.         else:
  54.             self.gzip = 0
  55.         return addinfourl.__init__(self, fp, headers, url)
  56.  
  57.     
  58.     def close(self):
  59.         self.fp.close()
  60.         if self.gzip:
  61.             self.rawfp.close()
  62.         
  63.  
  64.     
  65.     def iscompressed(self):
  66.         return self.gzip
  67.  
  68.  
  69.  
  70. class GzipStream(StringIO):
  71.     """Magically decompress a file object.
  72.  
  73.        This is not the most efficient way to do this but GzipFile() wants
  74.        to seek, etc, which won't work for a stream such as that from a socket.
  75.        So we copy the whole shebang info a StringIO object, decompress that
  76.        then let people access the decompressed output as a StringIO object.
  77.  
  78.        The disadvantage is memory use and the advantage is random access.
  79.  
  80.        Will mess with fixing this later.
  81.     """
  82.     
  83.     def __init__(self, fp):
  84.         self.fp = fp
  85.         compressed = StringIO()
  86.         r = fp.read()
  87.         while r:
  88.             compressed.write(r)
  89.             r = fp.read()
  90.         compressed.seek(0, 0)
  91.         gz = GzipFile(fileobj = compressed)
  92.         str = ''
  93.         r = gz.read()
  94.         while r:
  95.             str += r
  96.             r = gz.read()
  97.         compressed.close()
  98.         gz.close()
  99.         StringIO.__init__(self, str)
  100.         del str
  101.  
  102.     
  103.     def close(self):
  104.         self.fp.close()
  105.         return StringIO.close(self)
  106.  
  107.  
  108.  
  109. def test():
  110.     '''Test this module.
  111.  
  112.        At the moment this is lame.
  113.     '''
  114.     print 'Running unit tests.\n'
  115.     
  116.     def printcomp(fp):
  117.         
  118.         try:
  119.             if fp.iscompressed():
  120.                 print 'GET was compressed.\n'
  121.             else:
  122.                 print 'GET was uncompressed.\n'
  123.         except:
  124.             print "no iscompressed function!  this shouldn't happen"
  125.  
  126.  
  127.     print 'Trying to GET a compressed document...\n'
  128.     fp = urlopen('http://a.scarywater.net/hng/index.shtml')
  129.     print fp.read()
  130.     printcomp(fp)
  131.     fp.close()
  132.     print 'Trying to GET an unknown document...\n'
  133.     fp = urlopen('http://www.otaku.org/')
  134.     print fp.read()
  135.     printcomp(fp)
  136.     fp.close()
  137.  
  138. install_opener(build_opener(HTTPContentEncodingHandler))
  139. if __name__ == '__main__':
  140.     test()
  141.  
  142.